package com.mysoftbase.base.config;

import com.mysoftbase.base.holder.DynamicDataSourceContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * @Author: JohnCoding
 * @Description: 动态数据源配置类
 * @date: 2019/12/26 11:30
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    public DynamicDataSource(DataSource defalutDataSource, Map<Object,Object> targetDataSourceMap) {
        super.setDefaultTargetDataSource(defalutDataSource);
        super.setTargetDataSources(targetDataSourceMap);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceContextHolder.getNowDataSource();
    }
}
